<?php namespace App\Controllers\Phm\Phmtask;

use App\Controllers\Phm\TxController;

//访问地址：http://127.0.0.1/phm/public/index.php/phm/phmtask/phmtaskindex
class Phmtaskindex extends TxController
{
    function __construct()
    {
        parent::__construct();
        //创建数据库连接
        $this->sqlQuery = new \App\Models\Phm\Phmtask\ModelPhmtask();
        //此模块对应页面路径
        $this->localUrl = 'public/index.php/phm/phmtask/phmtaskindex/';
    }

    //phmtask
    public function index()
    {
        //显示数据
        echo view('phm/head', $this->menuUrl);
        echo view('phm/phmtask/phmtask', $this->menuUrl);
        echo view('phm/foot', $this->menuUrl);
    }

    public function del($AID = '')
    {
        if ($AID < 1) {
            return;
        }

        //头部
        echo view('phm/headsimple');

        $arrData = $this->sqlQuery->getPhmtaskById($AID);
        $arrData["ACTION_URL"] = base_url($this->localUrl . 'del/' . $AID);
        $arrData["MSG_OK"] = '';
        $arrData["MSG_FAULT"] = '';

        //执行删除
        if ($this->request->getPost('sub')) {
            if ($AID > 0) {
                //删除
                $m_Result = $this->sqlQuery->basedatDeleteByID($AID);
                //判定数据库删除是否成功
                if ($m_Result > 0) {
                    //成功
                    $arrData['MSG_OK'] = '删除成功！';
                } else {
                    //失败
                    $arrData['MSG_FAULT'] = '删除失败！';
                }
            }
        }

        //显示写作页面
        echo view('phm/phmtask/phmtaskdel', $arrData);

        //底部
        echo view('phm/footsimple');
    }

    public function mod($AID = '')
    {
        //检测传入的编号是否正确
        //$PKID = $this->sqlQuery->getBasedataPkidById($AID);
        //头部
        echo view('phm/headsimple');

        $arrData = array(
            'TITLE' => '',
            'TEXT' => '',
            'ACNO' => '',
            'ATA' => '',
            'ACTYPE' => '',
            'DATE' => '',
            'MSG_OK' => '',
            'MSG_FAULT' => '',
            'ACTION_URL' => base_url($this->localUrl . 'mod/') . $AID,
        );

        if ($this->request->getPost('sub')) {
            //数组成员存在就替换
            $arrData = array_merge($arrData,
                array(
                    'ID' => $AID,
                    'DATE' => F_StringToUnix(trim($this->request->getPost('F_DATE'))),
                    'TITLE' => trim($this->request->getPost('F_TITLE')),
                    'TEXT' => trim($this->request->getPost('F_TEXT')),
                    'ACNO' => trim($this->request->getPost('F_ACNO')),
                    'ATA' => trim($this->request->getPost('F_ATA')),
                    'ACTYPE' => trim($this->request->getPost('ACTYPE')),
                    'STATUE' => trim($this->request->getPost('STATUE')),
                )
            );
            //修改
            $rst = $this->sqlQuery->basedatMod($arrData);
            //判定数据库保存是否成功
            if ($rst > 0) {
                //成功
                $arrData['MSG_OK'] = '保存成功！';
            } else {
                //失败
                $arrData['MSG_FAULT'] = '保存成功！';
            }
        }

        $arrData = array_merge($arrData,
            $this->sqlQuery->getPhmtaskById($AID)
        );

        //显示写作页面
        echo view('phm/phmtask/phmtaskadd', $arrData);

        //底部
        echo view('phm/footsimple');
    }

    public function add()
    {
        //头部
        echo view('phm/headsimple');

        $arrData = array(
            'TITLE' => '',
            'ACNO' => '',
            'ATA' => '',
            'ACTYPE' => '',
            'TEXT' => '',
            'DATE' => '',
            'STATUE' => '',
            'MSG_OK' => '',
            'MSG_FAULT' => '',
            'ACTION_URL' => base_url($this->localUrl . 'add'),
        );

        if ($this->request->getPost('sub')) {
            //数组成员存在就替换
            $arrData = array_merge($arrData,
                array(
                    'DATE' => F_StringToUnix(trim($this->request->getPost('F_DATE'))),
                    'TITLE' => trim($this->request->getPost('F_TITLE')),
                    'TEXT' => trim($this->request->getPost('F_TEXT')),
                    'ACNO' => trim($this->request->getPost('F_ACNO')),
                    'ATA' => trim($this->request->getPost('F_ATA')),
                    'ACTYPE' => trim($this->request->getPost('ACTYPE')),
                    'STATUE' => trim($this->request->getPost('STATUE')),
                )
            );
            //插入成功后返回ID
            $rst = $this->sqlQuery->phmtaskAdd($arrData);
            //数据库保存是否成功
            if ($rst > 0) {
                //成功
                $arrData['MSG_OK'] = '保存成功！';
                $arrData['ACTION_URL'] = base_url($this->localUrl . 'mod/') . base64_encode($rst);
            } else {
                //失败
                $arrData['MSG_FAULT'] = '！！保存失败！！';
            }
        }

        echo view('phm/phmtask/phmtaskadd', $arrData);
        //底部
        echo view('phm/footsimple');
    }

    //基础数据
    public function action($APostDate = '', $APageIndex = 1)
    {
        //显示数据
        echo view('phm/head', $this->menuUrl);

        $QueryData = array(
            'KeyWord1' => '',
        );

        //判断查询条件，如果为空，那么查询添加添加TX，如果查询条件为TX，那么不编号。如果为其他，那么解密
        if (strlen($APostDate) < 3) {
            $APostDate = "TX";
            $postUrl = $APostDate;
        } else {
            $QueryData = json_decode(base64_decode($APostDate), TRUE);
            $postUrl = $APostDate;
        }

        if (($this->request->getPost("sub"))) {
            //关键字
            $QueryData = array(
                'KeyWord1' => trim($this->request->getPost('KeyWord1')),
            );
            //加密给分页
            $postUrl = base64_encode(json_encode($QueryData));
        }

        //取数量
        $countno = $this->sqlQuery->getPhmtaskCount($QueryData);

        //调用我们的Lib库文件
        $pageData = [
            'perpage' => 8,
            'part' => 7,
            'nowindex' => $APageIndex,
            'total' => $countno,
            'url' => $this->localUrl . 'action/' . $postUrl  //默认的基准URL
        ];
        $txpages1 = new \App\Libraries\TxpagesLib($pageData);

        //取数据
        $QueryData["txpages"] = $txpages1->show(4);
        $QueryData["limit"] = $txpages1->limit;
        $QueryData["arr"] = $this->sqlQuery->getPhmtask($QueryData);
        $QueryData["urlPost"] = base_url($this->localUrl . 'action');
        $QueryData["urlAdd"] = base_url($this->localUrl . 'add');
        $QueryData["urlMod"] = base_url($this->localUrl . 'mod/');
        $QueryData["urlDel"] = base_url($this->localUrl . 'del/');

        echo view('phm/phmtask/phmtask', $QueryData);
        echo view('phm/foot', $this->menuUrl);
    }


    //--------------------------------------------------------------------
}